package com.nexgo.payment.trans.operator.list;

import android.support.annotation.NonNull;

import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.aop.annotation.RunOnMainThread;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.OperatorInfo;
import com.nexgo.payment.util.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 操作员管理业务实现类
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年08月07日
 */
public class OperatorListPresenter implements OperatorListContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(OperatorListPresenter.class.getSimpleName());

    private OperatorListContract.View mView;
    private DataSource mRepository;
    private boolean mIsFirstTimes = true;

    public OperatorListPresenter(@NonNull OperatorListContract.View view, @NonNull DataSource repository) {
        mView = checkNotNull(view);
        mRepository = checkNotNull(repository);
        mView.setPresenter(this);
    }

    @Override
    public void start() {
        if (mIsFirstTimes) {
            String operatorNo = StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.CURRENT_OPERATOR_NO), "1");
            postMainThread(() -> mView.showCurrentOperator(StringUtils.leftPad(operatorNo, 2, '0')));
            getOperatorList();
            mIsFirstTimes = false;
        }
    }

    @Override
    public void getOperatorList() {
        //获取操作员列表
        List<OperatorInfo> mUserList = mRepository.getOperatorList();
        if (mUserList == null || mUserList.size() == 0) {
            postMainThread(() -> mView.showTips(App.sContext.getString(R.string.operator_list_is_empty)));
            return;
        }
        mLog.debug("操作员数量：{}", mUserList.size());
        postMainThread(() -> mView.showOperatorList(mUserList));
    }

    @Override
    public void addOperatorInfo() {
        //非主管不可添加操作员
        if (!isAdminOperator()) return;
        postMainThread(() -> mView.intent2AddOperator());
    }

    @Override
    public void editOperatorInfo(String operatorNo) {
        //非主管不可编辑
        //非当前签到操作员不可编辑(即不可编辑其他操作员信息)
        if (!isSelfOperator(operatorNo)) return;
        postMainThread(() -> mView.intent2EditOperator(operatorNo));
    }

    @Override
    public void confirmDelete(String operatorNo) {
        //非主管不可删除操作员
        if (!isAdminOperator()) return;
        postMainThread(() -> mView.showDialog(operatorNo));
    }

    @Override
    public void deleteOperatorInfo(String operatorNo) {
        mRepository.deleteOperatorByOperatorNo(operatorNo);
        postMainThread(() -> mView.showTips(App.sContext.getString(R.string.delete_success)));
        getOperatorList();
    }

    @Override
    public boolean isAdminOperator() {
        //取出当前操作员编号，取不到则默认为主管操作员编号
        String operatorNo = StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.CURRENT_OPERATOR_NO), "00");
        if (!"00".equals(operatorNo)) {
            postMainThread(() -> mView.showTips(App.sContext.getString(R.string.need_to_sign_by_admin_operator)));
            return false;
        }
        return true;
    }

    @Override
    public boolean isSelfOperator(String operatorNo) {
        //取出当前操作员编号，取不到则默认为主管操作员编号
        String curOperatorNo = StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.CURRENT_OPERATOR_NO), "00");
        if ("00".equals(curOperatorNo)) {
            return true;
        }
        if (!operatorNo.equals(curOperatorNo)) {
            postMainThread(() -> mView.showTips(App.sContext.getString(R.string.can_no_edit_the_other_operator_info)));
            return false;
        }
        return true;
    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {

    }

    @RunOnMainThread
    private void postMainThread(Runnable runnable) {
        runnable.run();
    }
}
